package com.shadow.bus.main.setting;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.shadow.bus.event.CheckVersionEvent;
import com.shadow.bus.event.LogoutEvent;
import com.shadow.bus.tools.UsbUtil;
import com.tepth.shadow.ui.dialog.sweet.DialogDelegate;
import com.tepth.shadow.ui.dialog.sweet.SweetAlertDialogDelegate;
import com.tepth.shadow.ui.recycler.BaseDecoration;
import com.tepth.shadow.ui.recycler.RecyclerViewNoBugLinearLayoutManager;
import com.zhangyu.shadow.core.delegate.ShadowDelegate;
import com.zhangyu.shadow.core.util.app.AppUtils;
import com.zhangyu.shadow.core.util.net.NetworkUtils;
import com.zhangyu.shadow.core.util.toast.ToastUtils;

import org.greenrobot.eventbus.EventBus;

import busfix.tepth.com.shadow_bus.R;
import busfix.tepth.com.shadow_bus.R2;
import butterknife.BindView;

/**
 * @author zhangyu
 * @date 2018/7/3
 */

public class SettingDelegate extends ShadowDelegate {
    public static SettingDelegate newInstance() {
        return new SettingDelegate();
    }

    @BindView(R2.id.recycler_view)
    RecyclerView mRecyclerView;
    DialogDelegate mDialogDelegate;
    @BindView(R2.id.toolbar)
    Toolbar mToolbar;

    @Override
    public Object setLayout() {
        return R.layout.delegate_setting;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mToolbar.setTitle("设置");
        mToolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop();
            }
        });

        mRecyclerView.setLayoutManager(new RecyclerViewNoBugLinearLayoutManager(getContext()));
        ItemAdapter itemAdapter = new ItemAdapter(new SettingDataConverter().converter(getContext()),
                getContext());
        mRecyclerView.setAdapter(itemAdapter);
        mRecyclerView.addItemDecoration(BaseDecoration.create(getContext()
                .getResources()
                .getColor(R.color.color_white_light), 10));
        mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (position == 4) {
                    if (!UsbUtil.hasUsbDevice(getActivity())) {
                        startActivity(new Intent(getContext(), NFCListActivity.class));
                    }
                    //更新
                } else if (position == 5) {
                    if (NetworkUtils.isConnected()) {
                        EventBus.getDefault().post(new CheckVersionEvent());
                    } else {
                        ToastUtils.showToast(getContext(), "当前网络不可用!请检查网络");
                    }
                    //更新
                }
                //else if (position == 6) {
                //切换
//                    final boolean isDebug = ShadowPreference.getAppFlag(SpKey.IS_DEBUG);
//                    if (mDialogDelegate == null) {
//                        mDialogDelegate = new SweetAlertDialogDelegate(getContext());
//                    }
//                    mDialogDelegate.showWarningDialog(isDebug ? "切换为正式服务器" : "切换为测试服务器", "确认将会重启应用!", new DialogDelegate.OnDialogListener() {
//                        @Override
//                        public void onClick() {
//                            ShadowPreference.setAppFlag(SpKey.IS_DEBUG, !isDebug);
//                            mDialogDelegate.clearDialog();
//                            AppUtils.restartAPP(getContext());
//                        }
//                    });
                //              }
                else if (position == 8) {
                    if (mDialogDelegate == null) {
                        mDialogDelegate = new SweetAlertDialogDelegate(getContext());
                    }
                    mDialogDelegate.showWarningDialog("退出登录", "确认退出登录？", new DialogDelegate.OnDialogListener() {
                        @Override
                        public void onClick() {
                            mDialogDelegate.clearDialog();
                            EventBus.getDefault().post(new LogoutEvent());
                        }
                    });
                    //退出登录
                } else if (position == 9) {
                    //退出程序
                    if (mDialogDelegate == null) {
                        mDialogDelegate = new SweetAlertDialogDelegate(getContext());
                    }
                    mDialogDelegate.showWarningDialog("退出程序", "确认退出程序？", new DialogDelegate.OnDialogListener() {
                        @Override
                        public void onClick() {
                            mDialogDelegate.clearDialog();
                            AppUtils.exit(getContext());
                        }
                    });
                }
            }
        });
    }

    @Override
    public boolean onBackPressedSupport() {
        pop();
        return true;
    }
}
